package com.sky.controller.admin;

import com.alibaba.fastjson.JSON;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
  @Autowired
  private OrderService orderService;
  @Autowired
  private WebSocketServer webSocketServer;
  
  
  /**
   * 订单搜索
   * @param queryDTO
   */
  @GetMapping("/conditionSearch")
  public Result<PageResult> conditionSearch(OrdersPageQueryDTO queryDTO) {
    log.info("订单查询：{}", queryDTO);
    PageResult page = orderService.conditionSearch(queryDTO);
    return Result.success(page);
  }
  
  /**
   * 订单详情
   * @return
   */
  @GetMapping("/details/{id}")
  public Result<Orders> detail(@PathVariable Long id) {
    OrderVO orderVO = orderService.details(id);
    return Result.success(orderVO);
  }
  
  /**
   * 获取订单状态数量统计。
   */
  @GetMapping("/statistics")
  public Result<OrderStatisticsVO> statistics() {
    OrderStatisticsVO orderStatisticsVO = orderService.statistics();
    return Result.success(orderStatisticsVO);
  }
  
  
  /**
   * 接单
   */
  @PutMapping("/confirm")
  public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
    orderService.confirm(ordersConfirmDTO);
    return Result.success();
  }
  
  /**
   * 派送
   */
  @PutMapping("/delivery/{id}")
  public Result delivery(@PathVariable Long id) {
    orderService.delivery(id);
    return Result.success();
  }
  
  /**
   * 完成订单
   */
  @PutMapping("/complete/{id}")
  @ApiOperation("完成订单")
  public Result complete(@PathVariable("id") Long id) {
    orderService.complete(id);
    return Result.success();
  }
  
  
  
  /**
   * test用户催单
   * @return
   */
  @GetMapping("/remind/{id}")
  public Result remind(@PathVariable Long id) {
    System.out.println("订单id：" + id);
    Map map = new HashMap<>();
    map.put("type", 2);
    map.put("orderId", id);
    map.put("content", "搞快点！！！");
    String jsonString = JSON.toJSONString(map);
    webSocketServer.sendToAllClient(jsonString);
    
    return Result.success();
  }
  
  
  
 
}
